<?php

/**
 *    商品属性管理控制器
 *
 *    @author    censhuixing
 */
class AttributeApp extends BackendApp
{
	var $_attribute_mod;
	var $attr_input_type = array(
	0 => '手工录入',
	1 => '单选属性',
	2 => '复选属性',
	3 => '列表属性',
	4 => '多行文本框',
	);

	function __construct()
	{
		$this->AttributeApp();
	}

	function AttributeApp()
	{


		parent::BackendApp();


		$this->_attribute_mod =& m('attribute');
	}

	/**
     *    商品品牌索引
     *
     *    @author    Hyber
     *    @return    void
     */
	function index()
	{
		$cid = intval($_GET['cid']);
		$attributes = $this->_attribute_mod->get_list($cid);
		if(is_array($attributes))
		{
			foreach ($attributes as $key=>$val)
			{			
				$attributes[$key]['attr_input_type_name'] = $this->attr_input_type[$val['attr_input_type']];
			}
		}
		/*echo "<pre>";
		print_r($attributes);die();
		echo "</pre>";*/
		$this->assign('cid',  $cid);
		$this->assign('attributes', $attributes);

		/* 导入jQuery的表单验证插件 */
		$this->import_resource(array(
		'script' => 'jqtable.js,inline_edit.js',
		'style'  => 'res:style/jqtreetable.css'
		));

		$this->display('attribute.index.html');
		//print_r($a);
	}
	/**
     *    新增商品品牌
     *
     *    @author    Hyber
     *    @return    void
     */
	function add()
	{
		if (!IS_POST)
		{

			$cid  =  intval($_GET['cid']);
			/* 显示新增表单 */
			$attribute = array(
			'sort_order' => 255,
			'attr_input_type' => 0,
			'attr_index' => 1,
			);
			$attr_index = array(
			0 => '不需要检索',
			1 => '关键字检索',
			2 => '范围检索',
			);
			
			$this->import_resource(array(
			'script' => 'jquery.plugins/jquery.validate.js'
			));
			$this->assign('cid', $cid);
			$this->assign('attr_index', $attr_index);
			$this->assign('attr_input_type', $this->attr_input_type);
			$this->assign('attribute', $attribute);

			$this->display('attribute.form.html');
		}
		else
		{
			$data = array();
            $data['attr_name']     = trim($_POST['attr_name']);
			$data['sort_order']    = intval($_POST['sort_order']);
			$data['attr_index']    = intval($_POST['attr_index']);
			$data['attr_input_type']= intval($_POST['attr_input_type']);
			$data['attr_values']    = trim($_POST['attr_values']);
			$data['cate_id'] = intval($_POST['cid']);

			/* 检查名称是否已存在 */
			if (!$this->_attribute_mod->unique(trim($data['attr_name']),$data['cate_id']))
			{
				$this->show_warning('该属性名称已经存在了，请您换一个');
				return;
			}
			if (!$brand_id = $this->_attribute_mod->add($data))  //获取brand_id
			{
				$this->show_warning($this->_attribute_mod->get_error());

				return;
			}


			$this->show_message('添加属性成功',
			'back_list',    'index.php?app=attribute&amp;cid='.$_POST['cid'],
			'continue_add', 'index.php?app=attribute&amp;act=add&amp;cid='.$_POST['cid']
			);
		}
	}

	/* 检查属性唯一 */
	function check_attr_name ()
	{
		$attr_name = empty($_GET['attr_name']) ? '' : trim($_GET['attr_name']);
		$cate_id   = empty($_GET['cid']) ? 0 : intval($_GET['cid']);
		$attr_id   = empty($_GET['id']) ? 0 : intval($_GET['id']);
		if (!$attr_name) {
			echo ecm_json_encode(false);
		}
		if ($this->_attribute_mod->unique($attr_name, $cate_id,$attr_id)) {
			echo ecm_json_encode(true);
		}
		else
		{
			echo ecm_json_encode(false);
		}
		return ;
	}

	/**
     *    编辑商品品牌
     *
     *    @author    Hyber
     *    @return    void
     */
	function edit()
	{
		$attr_id = isset($_GET['id']) ? intval($_GET['id']) : 0;
		$cate_id = isset($_GET['cid']) ? intval($_GET['cid']) : 0;
		if (!$attr_id)
		{
			$this->show_warning('no_such_brand');
			return;
		}
		if (!IS_POST)
		{
			$find_data     = $this->_attribute_mod->find($attr_id);
			if (empty($find_data))
			{
				$this->show_warning('no_such_brand');

				return;
			}
			$attribute    =   current($find_data);

			$cid  =  intval($_GET['cid']);
			$attr_index = array(
			0 => '不需要检索',
			1 => '关键字检索',
			2 => '范围检索',
			);

			$this->import_resource(array(
			'script' => 'jquery.plugins/jquery.validate.js'
			));

			$this->assign('cid', $attribute['cate_id']);
			$this->assign('attr_index', $attr_index);
			$this->assign('attr_input_type', $this->attr_input_type);
			$this->assign('attribute', $attribute);

			$this->display('attribute.form.html');
		}
		else
		{
			$data = array();
			$data['attr_name']     = trim($_POST['attr_name']);
			$data['sort_order']    = intval($_POST['sort_order']);
			$data['attr_index']    = intval($_POST['attr_index']);
			$data['attr_input_type']= intval($_POST['attr_input_type']);
			$data['attr_values']    = trim($_POST['attr_values']);
			$data['cate_id'] = intval($_POST['cid']);

			/* 检查名称是否已存在 */
			if (!$this->_attribute_mod->unique(trim($data['attr_name']),$cate_id, $attr_id))
			{
				$this->show_warning('该属性名称已经存在了，请您换一个');
				return;
			}
			$rows=$this->_attribute_mod->edit($attr_id, $data);
			if ($this->_attribute_mod->has_error())
			{
				$this->show_warning($this->_attribute_mod->get_error());

				return;
			}


			$this->show_message('编辑属性成功',
			'back_list',    'index.php?app=attribute&amp;cid='.$_POST['cid'],
			'edit_again', 'index.php?app=attribute&amp;act=edit&amp;id=' . $attr_id
			);
		}
	}

	//异步修改数据
	function ajax_col()
	{
		$id     = empty($_GET['id']) ? 0 : intval($_GET['id']);
		$column = empty($_GET['column']) ? '' : trim($_GET['column']);
		$value  = isset($_GET['value']) ? trim($_GET['value']) : '';
		$data   = array();

		if (in_array($column ,array('sort_order')))
		{
			$data[$column] = $value;

			if($this->_attribute_mod->edit($id, $data))
			{
				echo ecm_json_encode(true);
			}
		}
		else
		{
			return ;
		}
		return ;
	}

	function drop()
	{
		$attr_ids = isset($_GET['id']) ? trim($_GET['id']) : '';
		if (!$attr_ids)
		{
			$this->show_warning('no_such_navigation');

			return;
		}
		$attr_ids=explode(',',$attr_ids);
		if (!$this->_attribute_mod->drop($attr_ids))    //删除
		{
			$this->show_warning($this->_attribute_mod->get_error());

			return;
		}

		$this->show_message('删除属性成功');
	}


}

?>